<?php
namespace app\admin\model;
use think\Config;
use think\Db;
class LogsModel extends \app\common\model\CommonModel
{
    
    /**
     * 日志列表
     * 
     */
    public function list($params)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;
            $field = "a.userid,a.event,a.event_time,a.status,a.desc,b.username,a.username loginname";
            $sql = "SELECT {$field} FROM `log` a LEFT JOIN `user` b ON a.userid = b.userid WHERE 1=1 ";
            if(!empty($params['event'])){
                $sql .= "AND a.`event` = '{$params['event']}' ";
            }
            if(!empty($params['status'])){
                $sql .= "AND a.`status` = '{$params['status']}' ";
            }
            if(!empty($params['username'])){
                $sql .= "AND b.`username` LIKE '%{$params['username']}%' ";
            }
            $order = "ORDER BY a.event_time DESC LIMIT {$start},{$pagesize}";
            $csql = str_replace($field,'count(*)',$sql);
            $sql .= $order;
            $res = $this->getRes($sql);
            $count = $this->getCount($csql);
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }    
    }
}